package com.atguigu.realtime.utils;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.realtime.common.EDUConfig;
import org.apache.flink.api.java.tuple.Tuple2;

import java.sql.Connection;
import java.util.List;

public class DimUtil {
    // select * from t where id = 'v.id' and user_id = '10'
    public static List<JSONObject> getDimInfo(Connection connection, String tableName, String id){


        return getDimInfo(connection,tableName,new Tuple2<>("ID",id));
    }

    public static List<JSONObject> getDimInfo(Connection connection, String tableName, Tuple2<String,String>... tuple2s){
        StringBuilder sql = new StringBuilder();
        sql.append("select * from ")
                .append(EDUConfig.HBASE_SCHEMA)
                .append(".")
                .append(tableName)
                .append(" where ");
        for (int i = 0; i < tuple2s.length; i++) {
            sql.append(tuple2s[i].f0)
                    .append(" = '")
                    .append(tuple2s[i].f1)
                    .append("'");
            if (i < tuple2s.length - 1){
                sql.append(" and ");
            }
        }
        System.out.println(sql.toString());

        return PhoenixUtil.sqlQuery(connection, sql.toString(), JSONObject.class);
    }
}
